package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.BondConfigDTO;
import com.eastfair.venueservice.dto.BondConfigPageQuery;
import com.eastfair.venueservice.dto.BondConfigSaveDTO;
import com.eastfair.venueservice.dto.BondConfigUpdateDTO;
import com.eastfair.venueservice.entity.BondConfig;
import com.eastfair.venueservice.service.BondConfigService;
import com.eastfair.venueservice.vo.BondConfigVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 保证金配置
 * </p>
 *
 * @author dqq
 * @date 2023-04-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/bondConfig")
@Api(value = "BondConfig", tags = "保证金配置")
@PreAuth(replace = "venueservice:bondConfig:")
public class BondConfigController extends SuperController<BondConfigService, Long, BondConfig, BondConfigPageQuery, BondConfigSaveDTO, BondConfigUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private BondConfigService bondConfigService;

    @Override
    public void handlerResult(IPage<BondConfig> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<BondConfig> bondConfigList = list
                .stream()
                .map((map) -> BondConfig.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(bondConfigList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BondConfigPageQuery> params) {
        BondConfigPageQuery pageQuery = params.getModel();
    }

    /**
     * 查询保证金配置VO列表.
     *
     * @param bondConfigDTO the bond config dto
     * @return the r
     */
    @PostMapping("/query/bondConfig")
    public R<List<BondConfigVO>> queryBondConfigVO(@RequestBody BondConfigDTO bondConfigDTO) {
        log.info("queryBondConfigVO - 查询保证金配置VO列表, bondConfigDTO={}", bondConfigDTO);
        return R.successDef(bondConfigService.listBondConfigVO(bondConfigDTO));
    }

    /**
     * 展会ID，主场ID，面积查询保证金配置.
     *
     * @param exhibitionManageId the exhibition manage id
     * @param homeId             the home id
     * @param siteArea           the site area
     * @return the by site area
     */
    @GetMapping("/get/siteArea")
    public R<BondConfig> getBySiteArea(@RequestParam("exhibitionManageId") Long exhibitionManageId,
                                         @RequestParam("homeId") Long homeId,
                                         @RequestParam("siteArea") BigDecimal siteArea) {
        log.info("展会ID，主场ID，面积查询保证金配置, exhibitionManageId={}, homeId={}, siteArea={}", exhibitionManageId, homeId, siteArea);
        return R.successDef(bondConfigService.getBySiteArea(exhibitionManageId, homeId, siteArea));
    }

    /**
     * 批量保存保证金配置.
     *
     * @param list the list
     * @return the r
     */
    @SysLog("保存保证金配置")
    @PostMapping("/save/batch")
    public R<Boolean> saveBatch(@RequestBody List<BondConfigDTO> list) {
        log.info("saveBatch - 批量保存保证金配置, list={}", list);
        return R.successDef(bondConfigService.saveBatch(list));
    }
}
